package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupItemService;
import com.itheima.health.service.CheckgroupService;
import com.itheima.health.service.CheckitemService;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("mobile/setmeal")
public class SetmealController {


    @DubboReference
    private SetmealService setmealService;
    @DubboReference
    private CheckgroupService checkgroupService;
    @DubboReference
    private CheckGroupItemService checkGroupItemService;

    /**
     * @Author: 15041
     * @Date: 10:15 2021/1/11
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 预约列表查询所有
    */
    @GetMapping("getSetmeal")
    public Result getSetmeal(){
        log.info("移动端-预约套餐列表展示controller层");
        List<Setmeal> list = setmealService.findAll();
        //遍历集合，添加七牛云域名
        for (Setmeal setmeal : list) {
            //重新赋值img的内容，将七牛云域名拼接到图片名称前面，这样就能在网页中显示
            setmeal.setImg("http://qmi5v8lhm.hn-bkt.clouddn.com/"+setmeal.getImg());
        }
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS,list);
    }

    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("移动端-根据id查询套餐详情:{}",id);
        Setmeal setmeal = setmealService.findById(id);
        //获取查询到的检查组集合
        List<CheckGroup> checkGroups = setmeal.getCheckGroups();
        //创建一个空集合 用来接收查询到的数据
        List<CheckGroup> checkGroupAdd = new ArrayList<>();
        //遍历检查组集合得到单个的检查组
        for (CheckGroup checkGroup : checkGroups) {
            //根据得到的检查组查询完整的检查组内容
            CheckGroup group = checkgroupService.findById(checkGroup);
            //新建一个中间表对象
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            //将检查组的id赋值进去
            checkGroupItem.setCheckgroupId(group.getId());
            //通过检查组id查询中间表，查询到检查项的内容
            List<CheckItem> checkItems = checkGroupItemService.findByCheckItems(checkGroupItem);
            log.info("查到的检查项内容是:{}",checkItems);
            //将查到的检查项添加到检查组中
            group.setCheckItems(checkItems);
            //将封装好的内容添加到新建的集合中
            checkGroupAdd.add(group);
        }
        //将查到的数据替换 因为之前查到的只有id没有数据
        setmeal.setCheckGroups(checkGroupAdd);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

}
